package com.dhemery.mack.actions;

import org.eclipse.jface.action.Action;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;

import com.dhemery.mack.adapters.ImageDescriptorSource;
import com.dhemery.mack.application.MackPlugin;

public class SaveAction extends Action {
	private static final String name = "Save";
	public static final String ID = MackPlugin.COMMAND_ID_PREFIX + name;
	private static final String imagePath = "icons/16/save_edit.png";

	private final IWorkbenchWindow window;

	public SaveAction(IWorkbenchWindow window, ImageDescriptorSource imageDescriptorSource) {
		super(name);
		this.window = window;
		setId(ID);
		setActionDefinitionId(ID);
		setImageDescriptor(imageDescriptorSource.getImageDescriptor(imagePath));
	}

	private IWorkbenchPage getPage() {
		return window.getActivePage();
	}

	@Override
	public void run() {
		for(IEditorPart editor : getPage().getDirtyEditors()) {
			editor.doSave(null);
		}
	}
}
